Set-PSDebug -Trace 1
$ROOT=(Get-Item -Path .\).BaseName

function keystore{
   keytool -genkey -v -keystore _keystore -alias xxx -keyalg RSA -keysize 2048 -validity 10000
}

function preview{
   #&debug
   &release
   &logcat
}

function chk{
   $Class=(Get-ChildItem -path . -recurse -include *Tests.java)[0].Basename
   mvn test -Dtest=$Class
}

function mk-sdcard{
   mksdcard -l sdcard 128M ./sdcard.img
}

function rebuild{
   .\gradlew.bat clean assembleRelease
}

function release{
   .\gradlew.bat assembleRelease
   &uninstall
   &install-release
}

function debug{
   .\gradlew.bat assembleDebug
   &uninstall
   $apk=Get-ChildItem -path . -recurse -include *-debug.apk | Select-Object -First 1
   adb install $apk
}

function logcat{
   <#
   $apk=Get-ChildItem -path . -recurse -include *.apk | Select-Object -First 1
   $pkg=[regex]::match((aapt dump badging $apk | Select-Object -First 1| Select-String -Pattern "'([.a-z]+)'" | ForEach-Object { $_.Matches[0].Value }), "'([.a-z]+)'").Groups[1].Value
   adb logcat *:e |grep $pkg
   #>
   adb logcat *:e
}

function install-release{
   #$apk=(Get-ChildItem -path "./app/build/outputs/apk/release" -recurse -include *.apk)[0].name
   $apk=Get-ChildItem -path "./app/build/outputs/apk/release" -recurse -include *.apk
   adb install $apk
}

function new-project{
   android create project --target 22 --name "xxx" --path ./xxx --activity MainActivity  --package com.xxx
}

function sign{
   $apk=(Get-ChildItem -path . -recurse -include *.apk)[0].name

   java -jar uber-apk-signer.jar -a $apk --ks _keystore --ksAlias xxx --ksKeyPass xxxxxx --ksPass xxxxxx -o $apk
}

function guess-version{
   write-host (get-content (get-childItem -Path .. -Include build.gradle -File -Recurse -ErrorAction SilentlyContinue)| where {$_ -match "\s+version\s+'(.+)'"})
   $matches[1]
}

function repack{
   $apk=(Get-ChildItem -path . -recurse -include *.apk)[0].name
   apktool b extracted -o $apk
}

function uninstall{
   $apk=Get-ChildItem -path . -recurse -include *.apk | Select-Object -First 1
   $package=[regex]::match((aapt dump badging $apk | Select-Object -First 1| Select-String -Pattern "'([.a-z0-9]+)'" | ForEach-Object { $_.Matches[0].Value }), "'([.a-z0-9]+)'").Groups[1].Value
   adb uninstall $package
}

function signin{
   $apk=(Get-ChildItem -path . -recurse -include *-aligned-signed.apk)[0].name
   adb install $apk
}

function install{
   $apk=Get-ChildItem -path . -recurse -include *.apk | Select-Object -First 1
   #$apk=(Get-ChildItem -path . -recurse -include *.apk)[0].name
   adb install $apk
}

function depack{
   $apk=(Get-ChildItem -path . -recurse -include *.apk)[0].name
   apktool d "$apk" -o extracted
}

function boot{
   #$device_name=emulator -list-avds |Select-Object -Last 1
   $device_name=emulator -list-avds |Select-Object -First 1
   Start-Process "emulator" "@$device_name -sdcard ./sdcard.img"
}

function select-device{
   adb devices | Select-Object -First 2 | Select-String -Pattern '[a-z]+-[0-9]+' | ForEach-Object { $_.Matches[0].Value }
}

function clean{
   .\gradlew.bat clean
}

if ($args.Length -gt 0){ &$args[0] $args[1..$args.Length] }
